package com.easy.life.Listener;

import com.easy.life.dao.OrderDao;
import com.easy.life.dao.RoomDao;
import com.easy.life.entity.OrderEntity;
import com.easy.life.entity.RoomEntity;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {


    @Resource
    private OrderDao orderDao;
    @Resource
    private RoomDao roomDao;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对 redis 数据失效事件，进行数据处理
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {

        // 获取到失效的 key，进行取消订单业务处理
        String expiredKey = message.toString();
        if (expiredKey.length()<11) {
            OrderEntity orderEntity = orderDao.selectById(expiredKey);
            RoomEntity roomEntity = new RoomEntity();
            roomEntity.setStatus(1);
            roomEntity.setId(orderEntity.getRoomId());
            roomDao.updateById(roomEntity);
        }

    }

}
